package com.kob.backend.service.impl.auction;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kob.backend.mapper.AuctionTaskMapper;
import com.kob.backend.pojo.AuctionTask;
import com.kob.backend.pojo.TaskObj;
import com.kob.backend.service.auction.AuctionTaskService;
import com.kob.backend.vo.AuctionTaskVo;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>{@code
 *
 * }</pre>
 *
 * @author Summerday
 * @date 2023/3/17
 */
@Service
public class AuctionTaskServiceImpl extends ServiceImpl<AuctionTaskMapper, AuctionTask> implements AuctionTaskService {
    @Resource
    AuctionTaskMapper auctionTaskMapper;

    @Override
    public void deleteByAuctionId(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("auction_id", id);
        auctionTaskMapper.deleteByMap(map);
    }

    @Override
    public List<AuctionTaskVo> getByEVId(Integer id) {
        List<AuctionTask> auctionTasks = auctionTaskMapper.selectList(
                new QueryWrapper<AuctionTask>().eq("ev", id)
                        .orderByDesc("publish_time"));
        List<AuctionTaskVo> ans = new ArrayList<>();
        for(AuctionTask t : auctionTasks) {
            ans.add(AT2VO(t));
        }
        return ans;
    }

    @Override
    public AuctionTaskVo getOneById(Integer id) {
        List<AuctionTask> auctionTasks = auctionTaskMapper.selectList(
                new QueryWrapper<AuctionTask>().eq("id", id)
                        .orderByDesc("publish_time"));
        return AT2VO(auctionTasks.get(0));
    }

    @Override
    public List<AuctionTaskVo> getByRSUId(Integer id) {
        List<AuctionTask> auctionTasks = auctionTaskMapper.selectList(
                new QueryWrapper<AuctionTask>().eq("rsu", id)
                        .orderByDesc("publish_time"));
        List<AuctionTaskVo> ans = new ArrayList<>();
        for(AuctionTask t : auctionTasks) {
            ans.add(AT2VO(t));
        }
        return ans;
    }

    @Override
    public List<AuctionTaskVo> getList() {
        List<AuctionTask> auctionTasks = auctionTaskMapper.selectList(new QueryWrapper<AuctionTask>().select().orderByDesc(
                "publish_time"));
        List<AuctionTaskVo> ans = new ArrayList<>();
        for(AuctionTask t : auctionTasks) {
            ans.add(AT2VO(t));
        }
        return ans;
    }

    @Override
    public List<AuctionTaskVo> getByAuctionId(Integer id) {
        List<AuctionTask> auctionTasks = auctionTaskMapper.selectList(
                new QueryWrapper<AuctionTask>().eq("auction_id", id)
                        .orderByDesc("publish_time"));
        List<AuctionTaskVo> ans = new ArrayList<>();
        for(AuctionTask t : auctionTasks) {
            ans.add(AT2VO(t));
        }
        return ans;
    }

    @SneakyThrows
    private AuctionTaskVo AT2VO(AuctionTask task) {
        AuctionTaskVo vo = new AuctionTaskVo();
        vo.setAuctionId(task.getAuctionId());
        vo.setDescription(task.getDescription());
        vo.setEndTime(task.getEndTime());
        vo.setEv(task.getEv());
        vo.setTaskId(task.getTaskId());
        vo.setId(task.getId());
        String tasks = task.getTasks();
        ObjectMapper objectMapper = new ObjectMapper();
        List<TaskObj> taskObjs = objectMapper.readValue(tasks,
                objectMapper.getTypeFactory().constructCollectionType(List.class, TaskObj.class));
        double cost = 0;
        for(TaskObj taskObj : taskObjs) {
            // 计算能量消耗
            cost += taskObj.getPropertyCost() * taskObj.getPropertyValue();
            if(cost > 100) {
                cost = 100;
                break;
            }
        }
        vo.setCost(String.valueOf(cost));
        vo.setTasks(taskObjs);
        vo.setEvToRsu(task.getEvToRsu());
        vo.setRsuToEv(task.getRsuToEv());
        vo.setPublishTime(task.getPublishTime());
        vo.setRsu(task.getRsu());
        return vo;
    }
}
